<template>
  <KView class="group-bargain">
    <page-header title="拼团砍价" @back-click="toBack"></page-header>
    <KView class="group-bargain-content" v-if="isShow" :key="pageKey">
      <template v-if="search.timesList.length>0">
        <KView class="group-bargain-search">
          <search-times-view :search="search" @search-data="searchData"></search-times-view>
        </KView>
        <KView class="group-bargain-goods-view" :key="search.oldCurTimes.id">
          <group-bargain-goods :goods-list="goodsList" :cur-times="search.oldCurTimes" :pagination="pagination" :next-data-fun="setGoodsList"
            @type-change="typeChange">
          </group-bargain-goods>
        </KView>
      </template>

      <KView v-else class="group-bargain-nodata flex-central">
        <KView>活动正在筹备中，请耐心关注哦</KView>
      </KView>
    </KView>
  </KView>
</template>

<script>
  import SystemUtil from 'utils/systemUtil';
  import repeatRequest from 'mixins/repeatRequest';

  import PageHeader from 'common/pageHeader';
  import SearchTimesView from './searchTimesView';
  import GroupBargainGoods from './groupBargainGoods';

  import Pagination from 'models/pagination';
  import GroupBargainApi from 'services/groupBargainApi';

  export default {
    mixins: [repeatRequest],

    props: {
      hasBack: {
        type: Boolean,
        default: true
      }
    },

    data() {
      return {
        isShow: false,
        isMiniprogram: process.env.isMiniprogram,
        pageKey: SystemUtil.getId(),
        goodsList: [],
        search: null,
        pagination: null
      };
    },

    created() {
      SystemUtil.showLoading();
      this.initData().then(() => {
        SystemUtil.hideLoading();
        this.search.toChangeType();
        this.pageRunning();
      });
    },

    methods: {
      initData() {
        return this.setGroupBargainSearch().then(search => {
          if (search === null) {
            return search;
          }
          this.search = search;
          if (search.timesList.length === 0) {
            this.isShow = true;
            return search;
          }

          let pagination = new Pagination();
          return this.setGoodsList(true, pagination).then(() => {
            this.pagination = pagination;
            this.isShow = true;
            return search;
          });
        }).catch(() => {
          return null;
        });
      },

      setGroupBargainSearch() {
        return GroupBargainApi.getGroupBargainTimes().then(res => {
          let systemTime = res.systemTime;
          let nowTime = this.$moment().valueOf();
          let rangeTime = 60 * 1000;
          if (!this.$moment(systemTime).isBetween(nowTime - rangeTime, nowTime + rangeTime, 'second', '[]')) {
            SystemUtil.alert('本地时间与服务器时间不同步，请重新设置时间', '提示').then(() => {
              this.toBack();
            }).catch(() => {});
            return null;
          }
          return res.search;
        });
      },

      setGoodsList(isInit = false, pagination) {
        if (pagination === undefined) {
          pagination = this.pagination;
        }
        return GroupBargainApi.getGroupBargainGoodsList(this.search, pagination).then(goodsList => {
          if (isInit) {
            this.goodsList = goodsList;
          } else {
            this.goodsList.push(...goodsList);
          }
          return goodsList;
        });
      },

      searchData() {
        let pagination = new Pagination();
        SystemUtil.showLoading();
        this.setGoodsList(true, pagination).then(() => {
          this.pagination = pagination;
          this.search.oldCurTimes = this.search.curTimes;
          SystemUtil.hideLoading();
        }).catch(() => {
          SystemUtil.hideLoading();
        });
      },

      typeChange() {
        let curTimes = this.search.oldCurTimes;
        this.search.toChangeType(curTimes);
      },

      pageRunning() {
        this.clearTimeoutList();
        let nowTime = this.$moment().valueOf();
        let nexTime = this.$moment().startOf('hour').add(1, 'hour').valueOf();
        let rangeTime = nexTime - nowTime;
        this.timeoutList[0] = setTimeout(() => {
          if (this.isDestroy) {
            return;
          }
          this.search.toChangeType();
          this.timeoutList[0] = null;
          this.pageRunning();
        }, rangeTime);
      },

      toBack() {
        if (!this.isMiniprogram) {
          window.history.back();
          return;
        }

        if (this.hasBack) {
          wx.navigateBack();
        } else {
          this.$emit('to-page', '/home');
        }
      },

      resetData() {
        SystemUtil.showLoading('刷新中...');
        this.clearTimeoutList();
        return this.initData().then(search => {
          this.pageKey = SystemUtil.getId();
          this.search.toChangeType();
          this.pageRunning();
          SystemUtil.hideLoading();
          return search;
        });
      }
    },

    components: {
      PageHeader,
      SearchTimesView,
      GroupBargainGoods
    }
  };

</script>

<style lang="scss">
  .group-bargain {
    height: 100%;

    .page-header {
      margin-bottom: formatPx(20);
    }

    .group-bargain-content {
      height: calc(100% - #{formatPx(85)});

      .group-bargain-search {
        height: formatPx(200);
      }

      .group-bargain-goods-view {
        margin-top: formatPx(30);
        height: calc(100% - #{formatPx(240)});
      }

      .group-bargain-nodata {
        height: 100%;

        >div {
          color: #999999;
          width: 80%;
          text-align: center;
        }
      }
    }
  }

</style>
